package com.tuanzi.loan.business.repository.common.impl;

import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.repository.common.custom.CommProductIdRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.channel.CommProductIdRequest;
import com.tuanzi.loan.vo.response.channel.CommProductIdResponse;
import org.jooq.DSLContext;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;

/**
 * @author caiwanlin
 * @create 2018-10-18 14:08
 */
public class CommProductIdRepositoryImpl extends JOOQBaseRepositoryPlus implements CommProductIdRepositoryCustom {

    @Autowired
    private DSLContext dslContext;

    @Override
    protected TableImpl getTable() {
        return COMM_PRODUCT_PRDID;
    }

    @Override
    public Page<CommProductIdResponse> list(CommProductIdRequest searchRequest, Pageable pageable) {
        String name = searchRequest.getName();
        Integer productId = searchRequest.getProductId();
        fields(COMM_PRODUCT_PRDID.fields());
        eq(COMM_PRODUCT_PRDID.PRDID,productId);
        eq(COMM_PRODUCT_PRDID.PARENT_ID, searchRequest.getParentId());
        eq(COMM_PRODUCT_PRDID.ISAPP,searchRequest.getIsApp());
        contains(COMM_PRODUCT_PRDID.NAME,name);

        Page<CommProductIdResponse> search = search(pageable, record -> {
            CommProductIdResponse response = new CommProductIdResponse();
            response.setId(record.get(COMM_PRODUCT_PRDID.ID));
            response.setName(record.get(COMM_PRODUCT_PRDID.NAME));
            response.setProductId(record.get(COMM_PRODUCT_PRDID.PRDID));
            response.setParentId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));
            response.setIsApp(record.get(COMM_PRODUCT_PRDID.ISAPP));
            response.setProdCode(record.get(COMM_PRODUCT_PRDID.PROD_CODE));
            response.setMemo(record.get(COMM_PRODUCT_PRDID.MEMO));
            response.setExtraLogo(record.get(COMM_PRODUCT_PRDID.EXTRA_LOGO));
            response.setDownloadUrl(record.get(COMM_PRODUCT_PRDID.DOWNLOAD_URL));
            response.setWxDownloadUrl(record.get(COMM_PRODUCT_PRDID.WX_DOWNLOAD_URL));
            response.setPlatform(record.get(COMM_PRODUCT_PRDID.PLATFORM));
            response.setRealPrdid(record.get(COMM_PRODUCT_PRDID.REAL_PRDID));
            return response;
        });
        return search;
    }

    @Override
    public List<CommProductId> findAllAppProducts() {

        List<CommProductId> appCommProducts =  dslContext.select(COMM_PRODUCT_PRDID.fields())
                .from(COMM_PRODUCT_PRDID)
                .where(COMM_PRODUCT_PRDID.ISAPP.eq(1))
                .fetchInto(CommProductId.class);

        return appCommProducts;
    }
}
